#ifndef SRC_IICPS_H_
#define SRC_IICPS_H_

/***************************** Include Files **********************************/
#include "xparameters.h"
#include "xiicps.h"
#include "xil_printf.h"
#include "xscugic.h"
#include "xil_exception.h"
#include "sleep.h"
/************************** Constant Definitions ******************************/

// PCA9548 8-port IIC Switch
#define IIC_MUX_ADDRESS 	0x74 //b0_1110100
#define IIC_BUS_0 0x01
#define IIC_BUS_1 0x02
#define IIC_BUS_2 0x04
#define IIC_BUS_3 0x08
#define IIC_BUS_4 0x10
#define IIC_BUS_5 0x20
#define IIC_BUS_6 0x40
#define IIC_BUS_7 0x80

/*IIC SLAVE (DAC8571)*/
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID
#define IIC_INT_VEC_ID		XPAR_XIICPS_0_INTR
#define IIC_DEVICE_ID		XPAR_XIICPS_0_DEVICE_ID
#define IIC_SCLK_RATE		100000 //400Kbps
#define IIC_SLAVE_ADDR		0x4C //b0_1001100

/*IIC EEPROM (M24C08)*/
//#define EEPROM_ON_BORD
#define EEPROM_ON_CARD

#ifdef EEPROM_ON_BORD
	#define IIC_EEPROM_ADDR 		0x54//(rom on bord)
	#define EEPROM_ADDR_WIDTH		8
	#define IIC_MUX_SWITCH			IIC_BUS_2
#endif

#ifdef EEPROM_ON_CARD
	#define IIC_EEPROM_ADDR 		0x51//(rom on card)
	#define EEPROM_ADDR_WIDTH		16
	#define IIC_MUX_SWITCH			IIC_BUS_3
#endif

#define MAX_SIZE				16 //byte
#define PAGE_SIZE				MAX_SIZE
#define	ADDR_LEN				EEPROM_ADDR_WIDTH/8 //byte
#define EEPROM_START_ADDRESS	0

#define SLV_MON_LOOP_COUNT 		0x000FFFFF	/**< Slave Monitor Loop Count*/

/************************** Variable Definitions ******************************/
XIicPs 			I2C;
XScuGic 		InterruptController;
volatile u32 	SendComplete;
volatile u32 	RecvComplete;
volatile u32 	SlaveResponse;
volatile u32 	TotalErrorCount;

// EEPROM
typedef  u16 	AddressType;

enum rw_mode
{
	WRITE_THEN_READ,
	WRITE_ONLY,
	READ_ONLY
};
/************************ Function Prototypes *******************************/
int MuxInit(u8 channel);
int IicPsFindDevice(u16 Address);
int IicPsMasterInit(u8 mux_bus, u16 iic_addr);
int IicPsMasterSend(u8 *SendBuffer,s32 ByteCount);
int IicPsMasterRecv(u8 *SendBuffer,s32 ByteCount);
int IicPsConfig(u16 DeviceId, u32 Int_Id);
int SetupInterruptSystem(XIicPs *IicPsPtr, u32 Int_Id);
void Handler(void *CallBackRef, u32 Event);

// EEPROM
int EepromWriteData(u8 *WriteBuffer, u16 ByteCount);
int EepromWriteDataAddress(u8 *DataBuffer, u16 ByteCount, u16 StartAddr);
int EepromReadDataAddress(u8 *DataBuffer, u16 ByteCount, u16 StartAddr);


int  iic_rom_dac(void);
void cmd_rom_dac(void);

#endif /* SRC_IICPS_H_ */
